package com.ztf.clientThread;


import com.ztf.bean.DataTransfer;
import com.ztf.bean.Message;
import com.ztf.bean.User;
import com.ztf.gui.ChatRoom;
import com.ztf.gui.MsgListModel;

import java.io.ObjectInputStream;


public class AcceptThread extends Thread{
    private ObjectInputStream ois;
    private boolean flag;

    public AcceptThread(ObjectInputStream ois) {
        this.ois =ois;
        flag = true;
    }

    @Override
    public void run(){
        while (flag){
            try {
                DataTransfer o = (DataTransfer) ois.readObject();
                switch (o.getType()){
                    case "msg":
                        Message msg = (Message) o.getObject();
                        ChatRoom.msg.add(msg);
                        MsgListModel mo1 = new MsgListModel();
                        for (Message message : ChatRoom.msg) {
                            mo1.addElement(message);
                        }
                        ChatRoom.chatRoom.list_1.setModel(mo1);
                        ChatRoom.chatRoom.label7.setText("");

                        break;
                    case "for_you_server":
                        ChatRoom.customerService = (User) o.getObject();
                        Message message = new Message(ChatRoom.customerService, ChatRoom.userNow, System.currentTimeMillis(), ChatRoom.customerService.getName() + "为你服务");

                        sleep(500);
                        ChatRoom.msg.add(message);
                        MsgListModel mo2 = new MsgListModel();
                        for (Message messag2 : ChatRoom.msg) {
                            mo2.addElement(messag2);
                        }
                        ChatRoom.chatRoom.list_1.setModel(mo2);
                        break;
                    case "i_am_talking":
                        ChatRoom.chatRoom.label7.setText("对方正在输入");
                        break;
                }


            } catch (Exception e) {
                flag = false;
                System.out.println("接受错误");
                e.printStackTrace();
            }

        }
    }
}
